/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package at.irian.demo.jsfatwork.domain;

import static at.irian.demo.jsfatwork.domain.DomainQueryDetails.C_PARAMETER_USER;
import static at.irian.demo.jsfatwork.domain.DomainQueryDetails.FIND_USER_BY_NAME;
import at.irian.demo.jsfatwork.domain.validation.Severity;
import at.irian.demo.jsfatwork.domain.validation.UserName;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * @author JSF@Work Project Team
 */
@Entity
@Table(name = "T_USER")
@NamedQuery(name = FIND_USER_BY_NAME,
        query = "select u from User u where u.userName = " + C_PARAMETER_USER)
public class User extends Person
{
    private static final long serialVersionUID = -3821308345433991346L;

    @UserName
    @NotNull(payload = Severity.Fatal.class)
    @Size(min = 2, max = 9, payload = Severity.Fatal.class)
    @Column(nullable = false, length = 9, unique = true)
    private String userName;

    @NotNull(payload = Severity.Fatal.class)
    @Size(min = 2, max = 7, payload = Severity.Fatal.class)
    @Column(nullable = false, length = 7)
    private String password;

    @Temporal(TemporalType.DATE)
    @Column(nullable = false)
    private Date registrationDate;

    @PrePersist
    public void onRegistration()
    {
        this.registrationDate = new Date();
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public Date getRegistrationDate()
    {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate)
    {
        this.registrationDate = registrationDate;
    }

    void addPoll(Poll poll)
    {
        this.polls.add(poll);
    }

    void addGroup(Group group)
    {
        this.groups.add(group);
    }

    void removeGroup(Group group)
    {
        this.groups.remove(group);
    }

    @OneToMany(mappedBy = "owner")
    private Collection<Poll> polls = new ArrayList<Poll>();

    @OneToMany(mappedBy = "votedBy")
    private Collection<Vote> votes = new ArrayList<Vote>();

    //just for JQL queries
    @ManyToMany(mappedBy = "users")
    private Collection<Group> groups = new ArrayList<Group>();

    /*
     * generated
     */
    public Collection<Vote> getVotes()
    {
        return votes;
    }

    public void setVotes(Collection<Vote> votes)
    {
        this.votes = votes;
    }
}
